/**
 * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
 * This file is part of CSipSimple.
 *
 *  CSipSimple is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  If you own a pjsip commercial license you can also redistribute it
 *  and/or modify it under the terms of the GNU Lesser General Public License
 *  as an android library.
 *
 *  CSipSimple is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CSipSimple.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This file and this file only is also released under Apache license as an API file
 */

package com.cloudptt.api.pjsip.api;

import android.os.Parcel;
import android.os.Parcelable;

import com.cloudptt.api.comm.CheckData;
import com.mcptt.config.body.PocSettingsBody;
import com.mcptt.config.body.XCapBodyCms;
import com.mcptt.config.body.info.mcdata.InfoBodyPublishMcdata;
import com.mcptt.config.body.info.mcdata.McdataInfoBody;
import com.mcptt.config.body.info.mcptt.InfoBodyPublishMcptt;
import com.mcptt.config.body.info.mcptt.McpttInfoBody_Func1;
import com.mcptt.config.body.info.mcptt.McpttInfoBody_Func;
import com.mcptt.config.body.info.mcptt.McpttInfoBody_Location;
import com.mcptt.config.body.info.mcptt.McpttInfoBody_RequestCall;
import com.mcptt.config.body.info.mcvideo.InfoBodyPublishMcvideo;
import com.mcptt.config.body.pidf.PidfXmlBody;
import com.mcptt.config.body.pidf.PidfXmlBodyLogoutAll;
import com.mcptt.config.model.ImsInfo;
import com.mcptt.config.model.McIdInfo;

public class ImsProfile implements Parcelable {
    private String sbc =  null;
    private String dns =  null;
    private String impi = null;
    private String impu = null;

    private String mcpttId = null;
    private String mcvideoId = null;
    private String mcdataId = null;
    private String accessToken = null;
    private String clientId = null;

    private String domain ="crsc.mcx.mnc020.mcc460.5gr";
    private String psiCms ="sip:cms@crsc.mcx.mnc020.mcc460.5gr";
    private String psiGms = "sip:gms@crsc.mcx.mnc020.mcc460.5gr";
    private String psiMcptt = "sip:mcptt@crsc.mcx.mnc020.mcc460.5gr";
    private String psiMcvideo = "sip:mcvideo@crsc.mcx.mnc020.mcc460.5gr";
    private String psiMcdata = "sip:mcdata@crsc.mcx.mnc020.mcc460.5gr";
    public ImsProfile() {
    }

    public ImsProfile(McIdInfo mcIdInfo, ImsInfo info)
    {
        this.sbc =  info.getSbc();
        this.dns =  info.getDns();
        this.impi = info.getImpi();
        this.impu = info.getImpu();

        this.mcpttId = mcIdInfo.getMcpttId();
        this.mcvideoId = mcIdInfo.getMcvideoId();
        this.mcdataId = mcIdInfo.getMcdataId();
        this.accessToken = mcIdInfo.getAccessToken();
        this.clientId = mcIdInfo.getClientId();

        this.domain = info.getDomain();
        this.psiCms = info.getPsiCms();
        this.psiGms = info.getPsiGms();
        this.psiMcptt = info.getPsiMcptt();
        this.psiMcvideo = info.getPsiMcvideo();
        this.psiMcdata = info.getPsiMcdata();
    }

    private ImsProfile(Parcel in) {
        sbc = getReadParcelableString(in.readString());
        dns = getReadParcelableString(in.readString());
        impi = getReadParcelableString(in.readString());
        impu = getReadParcelableString(in.readString());

        mcpttId = getReadParcelableString(in.readString());
        mcvideoId = getReadParcelableString(in.readString());
        mcdataId = getReadParcelableString(in.readString());
        accessToken = getReadParcelableString(in.readString());
        clientId = getReadParcelableString(in.readString());

        domain = getReadParcelableString(in.readString());
        psiCms = getReadParcelableString(in.readString());
        psiGms = getReadParcelableString(in.readString());
        psiMcptt = getReadParcelableString(in.readString());
        psiMcvideo = getReadParcelableString(in.readString());
        psiMcdata = getReadParcelableString(in.readString());
    }

    public void updateAccessToken(String accessToken)
    {
        this.accessToken = accessToken;
    }


    /**
     * Parcelable creator. So that it can be passed as an argument of the aidl
     * interface
     */
    public static final Parcelable.Creator<ImsProfile> CREATOR = new Parcelable.Creator<ImsProfile>() {
        public ImsProfile createFromParcel(Parcel in) {
            return new ImsProfile(in);
        }

        public ImsProfile[] newArray(int size) {
            return new ImsProfile[size];
        }
    };

    /**
     * @see Parcelable#describeContents()
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * @see Parcelable#writeToParcel(Parcel, int)
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(getWriteParcelableString(sbc));
        dest.writeString(getWriteParcelableString(dns));
        dest.writeString(getWriteParcelableString(impi));
        dest.writeString(getWriteParcelableString(impu));

        dest.writeString(getWriteParcelableString(mcpttId));
        dest.writeString(getWriteParcelableString(mcvideoId));
        dest.writeString(getWriteParcelableString(mcdataId));
        dest.writeString(getWriteParcelableString(accessToken));
        dest.writeString(getWriteParcelableString(clientId));

        dest.writeString(getWriteParcelableString(domain));
        dest.writeString(getWriteParcelableString(psiCms));
        dest.writeString(getWriteParcelableString(psiGms));
        dest.writeString(getWriteParcelableString(psiMcptt));
        dest.writeString(getWriteParcelableString(psiMcvideo));
        dest.writeString(getWriteParcelableString(psiMcdata));
    }


    // Yes yes that's not clean but well as for now not problem with that.
    // and we send null.
    private String getWriteParcelableString(String str) {
        return (str == null) ? "null" : str;
    }

    private String getReadParcelableString(String str) {
        return str.equalsIgnoreCase("null") ? null : str;
    }
    public String getDomain() {
        return domain;
    }
    public String getSbcIp() {
        String[] strArr = sbc.split(":");
        return strArr[0];
    }
    public int getSbcPort() {
        String[] strArr = sbc.split(":");
        int port = CheckData.parseInt(strArr[1]);
        return port;
    }
    public String getSbc() {
        return sbc;
    }
    public String getDns() {
        return dns;
    }

    public String getImpi() {
        return impi;
    }

    public String getImpu() {
        return impu;
    }
    public String getImpuStr() {
        return new StringBuffer("<").append(impu).append(">").toString();
    }

    public String getMcpttId() {
        return mcpttId;
    }

    public String getMcvideoId() {
        return mcvideoId;
    }

    public String getMcdataId() {
        return mcdataId;
    }

    public String getClientId() {
        return clientId;
    }

    public String getPsiCms() {
        return psiCms;
    }

    public String getPsiGms() {
        return psiGms;
    }

    public String getPsiMcptt() {
        return psiMcptt;
    }

    public String getPsiMcvideo() {
        return psiMcvideo;
    }

    public String getPsiMcdata() {
        return psiMcdata;
    }

    public String toString()
    {

        StringBuffer strBuff = new StringBuffer("ImsProfile:");
        strBuff.append("sbc = ").append(sbc).append(",");
        strBuff.append("dns = ").append(dns).append(",");
        strBuff.append("impi = ").append(impi).append(",");
        strBuff.append("impu = ").append(impu).append(",");

        strBuff.append("mcpttId = ").append(mcpttId).append(",");
        strBuff.append("mcvideoId = ").append(mcvideoId).append(",");
        strBuff.append("mcdataId = ").append(mcdataId).append(",");
        strBuff.append("accessToken = ").append(accessToken).append(",");
        strBuff.append("clientId = ").append(clientId).append(",");

        strBuff.append("domain = ").append(domain).append(",");
        strBuff.append("psiCms = ").append(psiCms).append(",");
        strBuff.append("psiGms = ").append(psiGms).append(",");
        strBuff.append("psiMcptt = ").append(psiMcptt).append(",");
        strBuff.append("psiMcvideo = ").append(psiMcvideo).append(",");
        strBuff.append("psiMcdata = ").append(psiMcdata).append(";");

        return strBuff.toString();
    }

    public String toInfoBodyPublishMcptt()
    {
        InfoBodyPublishMcptt body = new InfoBodyPublishMcptt(mcpttId,accessToken,clientId);
        return body.toString();
    }
    public String toInfoBodyPublishMcvideo()
    {
        InfoBodyPublishMcvideo body = new InfoBodyPublishMcvideo(mcvideoId,accessToken,clientId);
        return body.toString();
    }
    public String toInfoBodyPublishMcdata()
    {
        InfoBodyPublishMcdata body = new InfoBodyPublishMcdata(mcdataId,accessToken,clientId);
        return body.toString();
    }

    public String toPocSettingBodyMcptt()
    {
        PocSettingsBody body = new PocSettingsBody(clientId);
        return body.toString();
    }
    public String toPocSettingBodyMcvideo()
    {
        PocSettingsBody body = new PocSettingsBody(clientId);
        return body.toString();
    }
    public String toPocSettingBodyMcdata()
    {
        PocSettingsBody body = new PocSettingsBody(clientId);
        return body.toString();
    }
    public String toSubscribeCmsGmsResourceBody(int mcType)
    {
        XCapBodyCms body = new XCapBodyCms(mcType,mcpttId,null);
        return body.toString();
    }

    public String toLocationMcpttInfoBody()
    {
        McpttInfoBody_Location body = new McpttInfoBody_Location(mcpttId);
        return body.toString();
    }

    public String toLocationMcpttInfoRequestCallBody(String type)
    {
        McpttInfoBody_RequestCall body = new McpttInfoBody_RequestCall(type,mcpttId);
        return body.toString();
    }
    public String toFuncMcpttInfoBody1()
    {
        McpttInfoBody_Func1 body = new McpttInfoBody_Func1(mcpttId);
        return body.toString();
    }
    public String toFuncMcpttInfoBody()
    {
        McpttInfoBody_Func body = new McpttInfoBody_Func(mcpttId);
        return body.toString();
    }
    public String toFuncPidfXmlBody(String uuId,String funcNum,boolean isForce,String initiateId)
    {
//        if(isForce) {
//            initiateId = AddrInfo.toUrlFromNum(initiateId);
//        }
//        funcNum = AddrInfo.toUrlFromNum(funcNum);
        PidfXmlBody body = new PidfXmlBody(uuId,mcpttId,funcNum,isForce,initiateId);
        return body.toString();
    }

    public String toFuncPidfXmlBodyLogoutAll(String[] funcNumList)
    {
        PidfXmlBodyLogoutAll body = new PidfXmlBodyLogoutAll(mcpttId,funcNumList);
        return body.toString();
    }

    public String toMcdataInfoBody(int type,String sessionType,String peerMcdataId)
    {

        McdataInfoBody body = new McdataInfoBody(type,sessionType,peerMcdataId,mcdataId,clientId);

        return body.toString();
    }

    public String toFuncMcdataInfoBody(int type,String sessionType,String peerMcdataId,String SenderFuncId)
    {

        McdataInfoBody body = new McdataInfoBody(type,sessionType,peerMcdataId,SenderFuncId,clientId);
        return body.toString();
    }

    public String toMcdataNotifyInfoBody(String controllerPsi,String groupId)
    {
        return McdataInfoBody.toMcdataInfoBody_Notify(controllerPsi,groupId);
    }


    public String toFdFindMcdataInfoBody(String sessionType)
    {
        McdataInfoBody body = new McdataInfoBody(sessionType);
        return body.toString();
    }
}
